//
//  TMHomeHeaderView.m
//  TMGold
//
//  Created by Mac on 2017/12/12.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMHomeHeaderView.h"
#import "STMenuControl.h"
@interface TMHomeHeaderView()
@property(nonatomic, strong) STMenuControl                     *buyControl;
@property(nonatomic, strong) STMenuControl                     *touziControl;
@property(nonatomic, strong) STMenuControl                     *inviteControl;
@property(nonatomic, strong) STMenuControl                     *zhuaGoldControl;
@end
@implementation TMHomeHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubView];
    }
    return self;
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.backgroundColor = [UIColor whiteColor];
    self.adverView = [[STAdvertingScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 153) andWithArray:@[@"banner01",@"banner02",@"banner03"] handle:^(NSInteger num) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(onslectedBannerWithIndex:)]) {
            [weakSelf.delegate onslectedBannerWithIndex:num];
        }
    }];
    self.adverView.pageControll.bottom = self.adverView.height - 15;
    self.adverView.pageControll.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.adverView.pageControll.pageIndicatorTintColor = TM_orangeColor;
    self.adverView.pageControll.centerX = UIScreenWidth / 2;
    self.adverView.canAutoScroll = YES;
    self.adverView.time = 3;
    [self addSubview:self.adverView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 20 + self.adverView.bottom, 300, 20)
                                                     text:@"今日金价(元/克)"
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self addSubview:titleLable];
    
    self.todayLable = [[STLabel alloc] initWithFrame:CGRectMake(15, titleLable.bottom + 5, 300, 40)
                                                     text:@"236.14↑0.88%"
                                                textColor:TM_redColor
                                                     font:28
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [self addSubview:self.todayLable];
    self.todayLable.attributedText = [self.todayLable.text st_convertAttributeStringWithKeyWord:@"↑0.88%" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:TM_orangeColor}];
    
    
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, self.adverView.bottom + 30, 90, 45)
                                                     title:@"买入"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:16
                                              cornerRadius:5
                                           backgroundColor:TM_orangeColor
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.right = UIScreenWidth - 15;
    buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [buyButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedBuyButton];
    }];
    [self addSubview:buyButton];
    
    UIView *  grayView = [[UIView alloc] initWithFrame:CGRectMake(0, buyButton.bottom + 30, UIScreenWidth, 10)];
    grayView.backgroundColor = TM_backgroundColor;
    [self addSubview:grayView];
    
    self.buyControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, grayView.bottom, UIScreenWidth / 4, 90) imageName:@"买黄金" title:@"买黄金" titleColor:TM_secendTextColor];
    [self.buyControl addTarget:self action:@selector(onSlectedControl:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.buyControl];
    
    self.touziControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, grayView.bottom, UIScreenWidth / 4, 90) imageName:@"黄金商城" title:@"黄金商城" titleColor:TM_secendTextColor];
    [self.touziControl addTarget:self action:@selector(onSlectedControl:) forControlEvents:UIControlEventTouchUpInside];
    self.touziControl.left = self.buyControl.right;
    [self addSubview:self.touziControl];
    
    self.inviteControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, grayView.bottom, UIScreenWidth / 4, 90) imageName:@"邀请好友" title:@"邀请好友" titleColor:TM_secendTextColor];
    [self.inviteControl addTarget:self action:@selector(onSlectedControl:) forControlEvents:UIControlEventTouchUpInside];
    self.inviteControl.left = self.touziControl.right;
    [self addSubview:self.inviteControl];
    
    self.zhuaGoldControl = [[STMenuControl alloc] initWithFrame:CGRectMake(0, grayView.bottom, UIScreenWidth / 4, 90) imageName:@"抓宝箱" title:@"抓宝箱" titleColor:TM_secendTextColor];
    [self.zhuaGoldControl addTarget:self action:@selector(onSlectedControl:) forControlEvents:UIControlEventTouchUpInside];
    self.zhuaGoldControl.left = self.inviteControl.right;
    [self addSubview:self.zhuaGoldControl];
    
    self.height = self.zhuaGoldControl.bottom + 0;
}
#pragma mark --Action Method
- (void)onSlectedBuyButton{
    if (self.delegate &&   [self.delegate respondsToSelector:@selector(onslectedResbonderWithTitle:)]) {
        [self.delegate onslectedResbonderWithTitle:@"买入"];
    }
}
- (void)onSlectedControl:(STMenuControl*)control{
    if (self.delegate &&   [self.delegate respondsToSelector:@selector(onslectedResbonderWithTitle:)]) {
        [self.delegate onslectedResbonderWithTitle:control.titleLable.text];
    }
}
@end
